import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class ClickPanelInPraise extends FlowPanel {
    Dimension lblSize = new Dimension(300, 60);
    Dimension btnSize = new Dimension(100, 60);
    Font font = new Font("Monospaced", Font.PLAIN, 24);
    private ArrayList<JComponent> components = new ArrayList<>();

    private JLabel lbl1 = new JLabel(FoodMenu.food[0] + " 点赞数：" + FoodMenu.praisenum[0]);
    private JButton btn1= new JButton("点赞") ;
    private int countPraise1 = 0;
    private JLabel lbl2 = new JLabel(FoodMenu.food[1] + " 点赞数：" + FoodMenu.praisenum[1]);
    private JButton btn2= new JButton("点赞") ;
    private int countPraise2 = 0;
    private JLabel lbl3 = new JLabel(FoodMenu.food[2] + " 点赞数：" + FoodMenu.praisenum[2]);
    private JButton btn3= new JButton("点赞") ;
    protected int countPraise3 = 0;
    private JButton btn4=new JButton("更多建议");

    {
        btn1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                countPraise1++;
                lbl1.setText(FoodMenu.food[0] + " 点赞数：" + (FoodMenu.praisenum[0] + countPraise1));
            }
        });
        btn2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                countPraise2++;
                lbl2.setText(FoodMenu.food[1] + " 点赞数：" + (FoodMenu.praisenum[1] + countPraise2));
            }
        });
        btn3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                countPraise3++;
                lbl3.setText(FoodMenu.food[2] + " 点赞数：" + (FoodMenu.praisenum[2] + countPraise3));
            }
        });

        components.add(lbl1);
        components.add(btn1);
        components.add(lbl2);
        components.add(btn2);
        components.add(lbl3);
        components.add(btn3);
        components.add(btn4);

        for (JComponent component: components){
            component.setFont(font);
            if (components.indexOf(component)%2==0){
                component.setPreferredSize(lblSize);
            } else {
                component.setPreferredSize(btnSize);
            }
            add(component);
        }addButton();
    }
    public ClickPanelInPraise(FlowLayout flowLayout){
        super(flowLayout);
    }

    public void setBtnSuggestListener(ActionListener actionListener){
        btn4.addActionListener(actionListener);
    }

    public void confirm(){
        FoodMenu.praisenum[0] += countPraise1;
        FoodMenu.praisenum[1] += countPraise2;
        FoodMenu.praisenum[2] += countPraise3;
    }

    public void clean(){
        countPraise1 = 0;
        countPraise2 = 0;
        countPraise3 = 0;
        lbl1.setText(FoodMenu.food[0] + " 点赞数：" + FoodMenu.praisenum[0]);
        lbl2.setText(FoodMenu.food[1] + " 点赞数：" + FoodMenu.praisenum[1]);
        lbl3.setText(FoodMenu.food[2] + " 点赞数：" + FoodMenu.praisenum[2]);

    }
}
